﻿using log4net;
using spring.stp6.db.provider;
using spring.stp6.db.provider.Entity;
using spring.stp6.web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.Mvc;

namespace spring.stp6.web.Controllers
{
    public class CompanyController : BaseController
    {
        private ILog logger = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private JsonData<CompanyEntity> data;

        Expression<Func<CompanyEntity, bool>> searchExpress;
        //
        // GET: /Company/       
        public ActionResult Index(int start,int limit)
        {
            try
            {
                searchExpress = u => u.status != 1;
                using (var ctx = new SalesManDBContent())
                {

                    List<CompanyEntity> list = ctx.Companys.Where(searchExpress)
                                                           .Select(u => u)
                                                           .OrderByDescending(u => u.id)
                                                           .Skip(start)
                                                           .Take(limit)
                                                           .ToList<CompanyEntity>();
                    data = new JsonData<CompanyEntity>(list.Count, list);
                }
            }
            catch (Exception ex)
            {
                data = new JsonData<CompanyEntity>();
                logger.Error("GET: /Company", ex);
            }
            return Json(data,JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Create(CompanyEntity e) {

            JsonMsg msg = new JsonMsg();
            try
            {
                using (var ctx = new SalesManDBContent())
                {
                    if (e.id > 0)
                    {
                        var selEntity = ctx.Companys.SingleOrDefault<CompanyEntity>(u => u.id == e.id);

                        selEntity.city = e.city;
                        selEntity.email = e.email;
                        selEntity.url = e.url;
                        selEntity.title = e.title;
                        selEntity.address = e.address;
                        selEntity.privilege = e.privilege;
                        selEntity.province = e.province;                       
                        selEntity.remarks = e.remarks;                        
                        selEntity.tele = e.tele;
                        selEntity.ctype = e.ctype;
                        ctx.SaveChanges();
                    }
                    else
                    {
                        e.createDate = DateTime.Now;
                        ctx.Companys.Add(e);
                        ctx.SaveChanges();
                    }
                }
                msg = new JsonMsg(true, "公司信息保存成功！");
            }
            catch (Exception ex)
            {
                logger.Error("POST: /Customer/Create", ex);
                msg = new JsonMsg(false, "公司信息保存失败！");
            }
            return Json(msg);
        }

    }
}
